package cn.shadow.module.chess.message.chnChess;

import cn.shadow.bean.PlayerModule;
import cn.shadow.module.chess.message.chnChess.frame.*;
import cn.shadow.module.snake.MyClickListener;
import cn.shadow.moulde.AHandler;
import cn.shadow.moulde.AModule;
import cn.shadow.moulde.Module;
import cn.shadow.proto.client.CHnChessClientProtoContent.*;
import cn.shadow.utils.BufferUtil;
import com.badlogic.gdx.Screen;
import com.badlogic.gdx.scenes.scene2d.Stage;
import io.netty.buffer.ByteBuf;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

/**
*
* Created time:2020-08-19 12:15:30
*/
@AModule(CHChessModuleMessages.MODULE_ID)
@Component
public class CHChessModule extends Module {


    @AHandler(CHChessModuleMessages.S2C_CHCHESS_CHOSEN)
    public void chosen(ByteBuf buffer, PlayerModule module) throws Exception {
            // 以下代码为自动生成，请不要随意修改
          final byte[] chosenData  =  BufferUtil.readBytes(buffer);  //棋局数据
            // 请在下面添加你的代码
         }

    @AHandler(CHChessModuleMessages.S2C_CHCHESS_CHECKMATE_TIP)
    public void checkmateTip(ByteBuf buffer, PlayerModule module) throws Exception {
            // 以下代码为自动生成，请不要随意修改
          final byte[] checkmateData  =  BufferUtil.readBytes(buffer);  //将军数据
            // 请在下面添加你的代码
         }

    @AHandler(CHChessModuleMessages.S2C_CHCHESS_MOVE)
    public void move(ByteBuf buffer, PlayerModule module) throws Exception {
            // 以下代码为自动生成，请不要随意修改
          final byte[] moveData  =  BufferUtil.readBytes(buffer);  //移动数据
            // 请在下面添加你的代码
         }

    @AHandler(CHChessModuleMessages.S2C_CHCHESS_INIT_CHESS)
    public void initChess(ByteBuf buffer, PlayerModule module) throws Exception {
            // 以下代码为自动生成，请不要随意修改
          final byte[] initChessData  =  BufferUtil.readBytes(buffer);  //初始化棋局数据
            // 请在下面添加你的代码
        CurrentBoard currentBoard = CurrentBoard.parseFrom(initChessData);
        List<ChessNode> chessNodeList = currentBoard.getChessNodeList();
        Screen screen = ChnChessGame.me().getScreen();
        if (screen instanceof ChnChessScreen){
            Stage controlStage = ((ChnChessScreen) screen).getControlStage();
        for (ChessNode chessNode : chessNodeList) {
            ChessMsg chess = chessNode.getChess();
            int chessCamp = chess.getChessCamp();
            int chessType = chess.getChessType();
            int x = chessNode.getPosX();
            int y = chessNode.getPosY();
            moduleLog.info(String.format("初始化,camp=%s,type=%s,x=%s,y=%s", Camp.getCampByValue(chessCamp), ChessType.getTypeByValue(chessType), x, y));
            ChessActor chessActor = ChessFactory.create(ChessType.getTypeByValue(chessType), Camp.getCampByValue(chessCamp));
            chessActor.setPosition(x,y);
            chessActor.addListener(new MyClickListener());
            controlStage.addActor(chessActor);
        }
    }
    }


}
